package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.PageResult;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.common.UserHolder;
import com.itheima.ydd.dto.AdvposDto;
import com.itheima.ydd.entity.Advimg;
import com.itheima.ydd.entity.Advpos;
import com.itheima.ydd.entity.User;
import com.itheima.ydd.service.AdvimgService;
import com.itheima.ydd.service.AdvposService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

//广告位
@Slf4j
@RequestMapping("/ydd_advpos")
@RestController
public class AdvposController {

    //广告位
    @Autowired
    private AdvposService advposService;

    //广告图
    @Autowired
    private AdvimgService advimgService;

    //分页查询
    @GetMapping("/list")
    private ResultData page(String advposdesc, String advposname, int page, int limit) {
        Page<Advpos> advposPage = new Page<>(page, limit);
        Page<AdvposDto> advposDtoPage = new Page<>();
        LambdaQueryWrapper<Advpos> lqw = new LambdaQueryWrapper<>();
        //条件
        lqw.like(advposdesc != null, Advpos::getAdvposdesc, advposdesc);
        lqw.like(advposname != null, Advpos::getAdvposname, advposname);
        advposService.page(advposPage,lqw);
        int count = advposService.count(lqw);

        BeanUtils.copyProperties(advposPage,advposDtoPage,"records");
        List<Advpos> advposList = advposPage.getRecords();
        //根据这个id统计广告图里的Advimgpos有多少个相同****理解
        List<AdvposDto> advposDtoslist = advposList.stream().map((it) -> {
            AdvposDto advposDto = new AdvposDto();
            BeanUtils.copyProperties(it, advposDto);
            //缺少图片数量  统计  id出现次数
            //这个里面统计id次数然后赋值给advposnum就好了啊
            LambdaQueryWrapper<Advimg> lqw1 = new LambdaQueryWrapper<>();
            lqw1.eq(Advimg::getAdvimgpos, it.getId());
            int count1 = advimgService.count(lqw1);
            advposDto.setAdvposnum(count1);
            return advposDto;
        }).collect(Collectors.toList());
        advposDtoPage.setRecords(advposDtoslist);
        PageResult pageResult = PageResult.init((long) count, Long.valueOf(limit), Long.valueOf(page), advposDtoslist);
        log.info("数据是{}",pageResult.toString());
        return ResultData.ok("", pageResult);
    }

    //添加
    @PostMapping("/add")
    public ResultData add(@RequestBody Advpos advpos) {
        advpos.setDefaultdata("1");//设置初始值
        advposService.save(advpos);
        return ResultData.ok("", "成功");
    }

    //删除
    @GetMapping("/del")
    public ResultData delete(int id) {
        //如果有广告图绑定了这个广告位就不能删除
        LambdaQueryWrapper<Advimg> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Advimg::getAdvimgpos, id);
        int count = advimgService.count(lqw);
        if (count>0){
            return ResultData.error("广告位有图片无法删除");
        }
        advposService.removeById(id);
        return ResultData.ok("", "成功");
    }

    //修改回显
    @GetMapping("/rowInfo")
    public ResultData row(int id) {
        Advpos byId = advposService.getById(id);
        return ResultData.ok("", byId);
    }

    //修改
    @PostMapping("/update")
    public ResultData update(@RequestBody Advpos advpos) {
        advposService.updateById(advpos);
        return ResultData.ok("", "成功");
    }

    //广告图的添加需要选择广告位 需要展示所有广告位名
    @GetMapping("/all")
    public ResultData all() {
        List<Advpos> list = advposService.list();
        return ResultData.ok("", list);
    }
}

